<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" @filterChange="handleSearch" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="borrowOrderRef"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      selectionType="single"
      :onRowSelectChange="selectChange"
      :onSyncTableData="handleSyncTableData"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>
    <div class="btnWrap">
      <el-button type="primary" @click="closeDrawer">取消</el-button>
      <el-button :loading="loading" type="primary" @click="saveHandler">保存</el-button>
    </div>
    <BaseDialog :visible.sync="showPanel" title="标题" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"> </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile, arrayObjDeWeight } from '@/utils/index';
import { findBorrwoOrderForReturn } from '@/api/part/tools';
import { getAllUser } from '@/api/customerRepair/usedCarVisit/resultEntry';
import { confirmAction, notifyAction } from '@/utils';

export default {
  mixins: [dictionary],
  mounted() {
    this.getAllUser();
  },
  data() {
    return {
      topFilterList: this.createTopFIlterList(),
      columns: this.createTableColumns(),
      fetchapi: findBorrwoOrderForReturn,
      fetchParams: { xhrAbort: true },
      loading: false,
      showPanel: false,
      selectRow: {},
      syncTableData: [] // 同步的table数据
    };
  },
  methods: {
    createTopFIlterList() {
      return [
        {
          type: 'INPUT',
          label: '借用单号',
          fieldName: 'borrowNo'
        },
        {
          type: 'RANGE_DATE',
          label: '借用日期',
          fieldName: 'start|end'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '借用单号',
          dataIndex: 'borrowNo',
          sorter: true
        },
        {
          title: '借用人',
          dataIndex: 'borrowUser',
          sorter: true
        },
        {
          title: '借用日期',
          filterType: 'input',
          dataIndex: 'borrowDate',
          valueFormat: 'yyyy-MM-dd',
          sorter: true
        }
      ];
    },
    collapseHandle() {
      this.nextTick(() => {
        this.refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    // 点击搜索
    handleSearch(e) {
      console.log('e:::', e);
      this.fetchParams = Object.assign({ xhrAbort: false }, e);
      this.fetchParams = { ...this.fetchParams };
    },
    closeDrawer() {
      this.$emit('close');
    },
    saveHandler() {
      let length = Object.keys(this.selectRow).length;
      if (length <= 0) {
        this.$notify({
          title: '失败',
          message: '请选择数据',
          type: 'warning'
        });
        return;
      }
      this.$emit('confirm', this.selectRow);
      this.$notify({
        title: '成功',
        message: '选择成功',
        type: 'success'
      });
      this.closeDrawer();
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    },
    async getAllUser() {
      let res = await getAllUser();
      let data = arrayObjDeWeight(res.data, 'userId');
      this.columns.find(item => item.dataIndex === 'borrowUser').dictItems = data.map(x => ({ value: x.userId + '', text: x.employeeName }));
    },
    selectChange(val) {
      this.selectRow = val[0];
    }
  }
};
</script>

<style lang="less" scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
